<!DOCTYPE html>

<html>
<head>
  <title>rotate.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="analyze.html">
                analyze.coffee
              </a>
            
              
              <a class="source" href="autoload.html">
                autoload.coffee
              </a>
            
              
              <a class="source" href="blender.html">
                blender.coffee
              </a>
            
              
              <a class="source" href="calculate.html">
                calculate.coffee
              </a>
            
              
              <a class="source" href="caman.html">
                caman.coffee
              </a>
            
              
              <a class="source" href="convert.html">
                convert.coffee
              </a>
            
              
              <a class="source" href="event.html">
                event.coffee
              </a>
            
              
              <a class="source" href="filter.html">
                filter.coffee
              </a>
            
              
              <a class="source" href="io.html">
                io.coffee
              </a>
            
              
              <a class="source" href="layer.html">
                layer.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="module.html">
                module.coffee
              </a>
            
              
              <a class="source" href="pixel.html">
                pixel.coffee
              </a>
            
              
              <a class="source" href="plugin.html">
                plugin.coffee
              </a>
            
              
              <a class="source" href="renderer.html">
                renderer.coffee
              </a>
            
              
              <a class="source" href="store.html">
                store.coffee
              </a>
            
              
              <a class="source" href="util.html">
                util.coffee
              </a>
            
              
              <a class="source" href="blenders.html">
                blenders.coffee
              </a>
            
              
              <a class="source" href="filters.html">
                filters.coffee
              </a>
            
              
              <a class="source" href="size.html">
                size.coffee
              </a>
            
              
              <a class="source" href="blur.html">
                blur.coffee
              </a>
            
              
              <a class="source" href="camera.html">
                camera.coffee
              </a>
            
              
              <a class="source" href="compoundBlur.html">
                compoundBlur.coffee
              </a>
            
              
              <a class="source" href="edges.html">
                edges.coffee
              </a>
            
              
              <a class="source" href="posterize.html">
                posterize.coffee
              </a>
            
              
              <a class="source" href="presets.html">
                presets.coffee
              </a>
            
              
              <a class="source" href="rotate.html">
                rotate.coffee
              </a>
            
              
              <a class="source" href="stackBlur.html">
                stackBlur.coffee
              </a>
            
              
              <a class="source" href="threshold.html">
                threshold.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>rotate.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Caman.Plugin.register <span class="string">"rotate"</span>, (degrees) -&gt;
  angle = degrees%<span class="number">360</span>
  <span class="keyword">if</span> angle == <span class="number">0</span> 
    <span class="keyword">return</span> <span class="property">@dimensions</span> =
      width: <span class="property">@canvas</span>.width
      height: <span class="property">@canvas</span>.height 
  to_radians = Math.PI/<span class="number">180</span>;
 
  <span class="keyword">if</span> exports?
    canvas = <span class="keyword">new</span> Canvas()
  <span class="keyword">else</span>
    canvas = document.createElement <span class="string">'canvas'</span>  
    Util.copyAttributes <span class="property">@canvas</span>, canvas
  
  <span class="keyword">if</span> angle == <span class="number">90</span> <span class="keyword">or</span> angle == -<span class="number">270</span> <span class="keyword">or</span> angle == <span class="number">270</span> <span class="keyword">or</span> angle == -<span class="number">90</span>
    width = <span class="property">@canvas</span>.height
    height = <span class="property">@canvas</span>.width
    x = width/<span class="number">2</span>
    y = height/<span class="number">2</span>
  <span class="keyword">else</span> <span class="keyword">if</span> angle == <span class="number">180</span>
    width = <span class="property">@canvas</span>.width
    height = <span class="property">@canvas</span>.height
    x = width/<span class="number">2</span>
    y = height/<span class="number">2</span> 
  <span class="keyword">else</span> 
    width = Math.sqrt(Math.pow(<span class="property">@originalWidth</span>, <span class="number">2</span>) + Math.pow(<span class="property">@originalHeight</span>, <span class="number">2</span>))
    height = width
    x = <span class="property">@canvas</span>.height/<span class="number">2</span>
    y = <span class="property">@canvas</span>.width/<span class="number">2</span>
  
  canvas.width = width
  canvas.height = height
  ctx = canvas.getContext <span class="string">'2d'</span>
  ctx.save()
  ctx.translate x, y	
  ctx.rotate angle*to_radians
  ctx.drawImage <span class="property">@canvas</span>, -<span class="property">@canvas</span>.width<span class="regexp">/2, -@canvas.height/</span><span class="number">2</span>, <span class="property">@canvas</span>.width, <span class="property">@canvas</span>.height
  ctx.restore()

  <span class="property">@replaceCanvas</span> canvas

Caman.Filter.register <span class="string">"rotate"</span>, -&gt;
  <span class="property">@processPlugin</span> <span class="string">"rotate"</span>, Array.prototype.slice.call(arguments, <span class="number">0</span>)</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
